Home:ALL Converter>Private/Protected variable "error: within this context"

Private/Protected variable "error: within this context"

Ask Time:2013-05-03T04:14:17         Author:user2184047

Json Formatter

I have a class based off of the SFML gamefromscratch.com tutorials, called "VisibleGameObject" Within this class, is a private variable "_sprite", as well as a "getSprite()" function that I tried as both protected and public. (Even as public, it still says "_sprite" is private even though the public function returns the variable).

In my OnRender class, I create two VisibleGameObjects.

VisibleGameObject _testtile1;
VisibleGameObject _cursorSprite;

But when I draw to draw the sprites, I get the error: within this context.

_mainWindow.draw(_cursorSprite._sprite);

alternatively I tried (with getSprite() being protected or public).

_mainWindow.draw(_cursorSprite.getSprite());

Yet always, "error: 'sf::Sprite VisibleGameObject::_sprite' is private. error: within this context"

Doesn't make any sense to me, because

1) _sprite is a variable of VisibleGameObject. It may be private, but it is not being accessed by anything but its own original class of "VisibleGameObject". I thought classes could access their own variables, even when they're a new instantiated object in another class?

2) getSprite() is public, and returns the private variable, yet it is STILL saying _sprite is private? This makes no sense to me! Everything I have learned about Getters and Setters, says that the public function CAN return a private variable, as that is the whole point of this Getter.

sf::Sprite& VisibleGameObject::getSprite()
{
return _sprite;
}




class VisibleGameObject
{
public:
VisibleGameObject();
virtual ~VisibleGameObject();

 private:
sf::Sprite  _sprite;



protected:
sf::Sprite& getSprite();

OR

public:
sf::Sprite& getSprite();

Author:user2184047,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/16346919/private-protected-variable-error-within-this-context
yy